package com.atguigu.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service

public class PaymentService {
    /**
     * 正常访问的方法
     * @param id
     * @return
     */
    public String paymentInfo_OK(String id){
        return "线程池   "+Thread.currentThread().getName() +" payment_ok, id:  "+id;
    }

    @HystrixCommand(fallbackMethod = "paymentInfoHanlder",
    commandProperties = {@HystrixProperty(name =  "execution.isolation.thread.timeoutInMilliseconds",value = "6000")})
    public String paymentInfo_Timeout(String id){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        int i= 1/0;
        return "线程池   "+Thread.currentThread().getName() +" payment_timeOut, id:  "+id;
    }

    //自动调用fallback中的方法
    public String paymentInfoHanlder(String id){
        return "系统繁忙:线程池   "+Thread.currentThread().getName() +" payment_timeOut, id:  "+id+"   fallback:  enter falback";
    }
}
